package com.clear.club.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.clear.club.circle.api.common.Result;
import com.clear.club.circle.api.enums.IsDeletedFlagEnum;
import com.clear.club.circle.api.req.GetShareCommentReplyReq;
import com.clear.club.circle.api.req.RemoveShareCommentReplyReq;
import com.clear.club.circle.api.req.SaveShareCommentReplyReq;
import com.clear.club.circle.api.vo.ShareCommentReplyVO;
import com.clear.club.circle.server.entity.po.ShareCommentReply;
import com.clear.club.circle.server.entity.po.ShareMoment;
import com.clear.club.circle.server.sensitive.WordFilter;
import com.clear.club.circle.server.service.ShareMessageService;
import com.clear.club.circle.server.service.IShareMomentService;
import com.clear.club.circle.server.service.ShareCommentReplyService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 评论及回复信息(ShareCommentReply)表控制层
 *
 * @author makejava
 * @since 2024-07-03 18:05:53
 */
@RestController
@RequestMapping("/circle/share/comment")
@Slf4j
public class ShareCommentReplyController {

    @Resource
    private IShareMomentService shareMomentService;

    @Resource
    private ShareCommentReplyService shareCommentReplyService;

    @Resource
    private WordFilter wordFilter;

    @Resource
    private ShareMessageService shareMessageService;

    /**
     * 发布内容  评论或回复
     */
    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody SaveShareCommentReplyReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("发布评论内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getMomentId()), "原始动态id不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "回复类型不能为空！");  // 这是判断是 评论 回复 的重要依据
            Preconditions.checkArgument(Objects.nonNull(req.getTargetId()), "评论目标id不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getContent()) || !CollectionUtils.isEmpty(req.getPicUrlList()), "评论内容不能为空！");
            // 判断原始动态是否还存在：不论是 评论 回复 都需要此步骤
            ShareMoment shareMoment = shareMomentService.getOne(Wrappers.<ShareMoment>lambdaQuery()
                    .eq(ShareMoment::getId, req.getMomentId())
                    .eq(ShareMoment::getIsDeleted, IsDeletedFlagEnum.UN_DELETED.getCode())
            );
            Preconditions.checkArgument(Objects.nonNull(shareMoment), "非法！原始动态不存在或已被删除！");
            // 敏感词检测
            if (Objects.nonNull(req.getContent())) {
                wordFilter.check(req.getContent());
            }
            Boolean result = shareCommentReplyService.saveComment(req);
            // todo websocket
            if (req.getReplyType() == 1) {   // 评论
                // todo 暂时写死
                shareMessageService.comment("小黑子卤出鸡脚", shareMoment.getCreatedBy(), shareMoment.getId());
            } else {    // 回复
                // a.查询回复的评论的创建人
                LambdaQueryWrapper<ShareCommentReply> queryWrapper = Wrappers.<ShareCommentReply>lambdaQuery()
                        .eq(ShareCommentReply::getId, req.getTargetId())
                        .select(ShareCommentReply::getCreatedBy);
                ShareCommentReply comment = shareCommentReplyService.getOne(queryWrapper);
                // b.
                // todo 暂时写死
                shareMessageService.reply("小黑子卤出鸡脚", comment.getCreatedBy(), shareMoment.getId());
            }
            if (log.isInfoEnabled()) {
                log.info("发布评论内容出参{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("发布评论内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("发布内容评论异常！");
        }

    }


    /**
     * 删除评论或回复
     * 说明：回复也是评论的一种形式，下面日志打印全部以`评论`一词概括
     */
    @PostMapping(value = "/remove")
    public Result<Boolean> remove(@RequestBody RemoveShareCommentReplyReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("删除评论内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(!Objects.isNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "评论内容id不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "回复类型不能为空！");  // 这是判断是 评论 回复 的重要依据

            Boolean result = shareCommentReplyService.removeComment(req);
            if (log.isInfoEnabled()) {
                log.info("删除评论内容出参{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("删除评论内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("删除评论内容异常！");
        }
    }

    /**
     * 查询该动态下的评论
     */
    @PostMapping(value = "/list")
    public Result<List<ShareCommentReplyVO>> list(@RequestBody GetShareCommentReplyReq req) {
        try {
            if (log.isInfoEnabled()) {
                log.info("获取动态评论内容入参{}", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "参数不能为空！");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "动态内容id不能为空！");
            List<ShareCommentReplyVO> result = shareCommentReplyService.listComment(req);
            if (log.isInfoEnabled()) {
                log.info("获取动态评论内容{}", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e) {
            log.error("参数异常！错误原因{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        } catch (Exception e) {
            log.error("获取动态评论内容异常！错误原因{}", e.getMessage(), e);
            return Result.fail("获取动态评论内容异常！");
        }
    }
}

